package user

import (
	"context"
	"fast-boot/app/api/admin/internal/svc"
	"fast-boot/app/api/admin/internal/types"
	"fast-boot/app/model"
	"github.com/hwUltra/fb-tools/gormx"

	"github.com/zeromicro/go-zero/core/logx"
)

type ThirdPageLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 获取第三方用户列表
func NewThirdPageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ThirdPageLogic {
	return &ThirdPageLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ThirdPageLogic) ThirdPage(req *types.UserPageReq) (resp *types.UserThirdPageRsqp, err error) {
	gModel := model.UserThirdModel{}

	total := int64(0)
	if err := l.svcCtx.GormClient.GormDb.Model(gModel).
		Scopes(gModel.WithKeywords(req.Keywords)).
		Count(&total).Error; err != nil {
		return nil, err
	}
	list := make([]*types.UserThird, 0)
	if total > 0 {
		l.svcCtx.GormClient.GormDb.Model(gModel).
			Scopes(
				gormx.Paginate(int(req.PageNum), int(req.PageSize)),
				gModel.WithKeywords(req.Keywords)).
			Order("id asc").Find(&list)
	}

	return &types.UserThirdPageRsqp{
		Total: total,
		List:  list,
	}, nil
}
